package com.keyroy.game.puzzle_rpg.views.tables;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.keyroy.game.puzzle_rpg.dialogs.ParamEditor;
import com.keyroy.game.puzzle_rpg.models.attribute.Param;
import com.keyroy.game.puzzle_rpg.models.attribute.Require;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.utils.UIUtil;

public class RequireTableView extends Composite {
	private Require require;
	private Table table;

	public RequireTableView(Composite parent) {
		super(parent, SWT.NONE);

		setLayout(new FormLayout());

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		FormData fd_toolBar = new FormData();
		fd_toolBar.bottom = new FormAttachment(100, -10);
		fd_toolBar.left = new FormAttachment(0, 10);
		toolBar.setLayoutData(fd_toolBar);

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = table.getSelectionIndex();
				if (index != -1) {

					EventListener<Param> listener = new EventListener<Param>() {

						@Override
						public void onEvent(Param t) {
							require.set(t);
							reset();
						}
					};
					UIUtil.open(new ParamEditor(getDisplay(), require.get(index), false, listener));
				}
			}
		});
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100, -39);
		fd_table.top = new FormAttachment(0);
		fd_table.left = new FormAttachment(0);
		fd_table.right = new FormAttachment(100);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				EventListener<Param> listener = new EventListener<Param>() {

					@Override
					public void onEvent(Param t) {
						require.set(t);
						reset();
					}
				};
				UIUtil.open(new ParamEditor(getDisplay(), false, listener));
			}
		});
		tltmNewItem.setImage(SWTResourceManager.getImage(ParamTableView.class, "/res/add_obj.gif"));
		tltmNewItem.setText("\u6DFB\u52A0");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getSelectionIndex() != -1) {
					require.remove(table.getSelectionIndex());
					reset();
				}

			}
		});
		tltmNewItem_1.setImage(SWTResourceManager.getImage(ParamTableView.class, "/res/remove_correction.gif"));
		tltmNewItem_1.setText("\u5220\u9664");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getSelectionIndex() != -1) {

					EventListener<Param> listener = new EventListener<Param>() {

						@Override
						public void onEvent(Param t) {
							require.set(t);
							reset();
						}
					};
					UIUtil.open(new ParamEditor(getDisplay(), require.get(table.getSelectionIndex()), false, listener));
				}
			}
		});
		tltmNewItem_2.setImage(SWTResourceManager.getImage(RequireTableView.class, "/res/debugt_obj.gif"));
		tltmNewItem_2.setText("\u8BBE\u7F6E");
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(71);
		tblclmnNewColumn.setText("\u6761\u4EF6/\u8981\u6C42");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(50);
		tblclmnNewColumn_1.setText("\u6570\u503C");

		set(new Require());
	}

	public void set(Require require) {
		if (require != null) {
			this.require = require;
			reset();
		}
	}

	public Require getRequire() {
		return require;
	}

	public void reset() {
		table.removeAll();
		if (require != null) {
			for (int i = 0; i < require.params.size(); i++) {
				Param param = require.params.get(i);
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(param.getString());
			}
		}
	}

	@Override
	protected void checkSubclass() {
	}

}
